<template>
  <div class="app-container">
    <div class="section-box">

      <el-row :gutter="10"
        class="mb8">
        <el-col :span="1.5">
          <el-button type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd">新增</el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading"
        :data="dataList">
        <el-table-column label="姓名"
          align="center"
          prop="famousName"
          :show-overflow-tooltip="true"
          min-width="160" />
        <el-table-column label="头衔"
          align="center"
          prop="famousTitle"
          :show-overflow-tooltip="true"
          min-width="160" />
        <el-table-column label="年代"
          align="center"
          prop="refAuthor"
          min-width="120">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.yj_dynasty"
              :value="scope.row.refAuthor" />
          </template>
        </el-table-column>
        <el-table-column label="简介"
          align="center"
          prop="brief"
          :show-overflow-tooltip="true"
          min-width="160" />
        <el-table-column label="头像"
          align="center"
          prop="images"
          :show-overflow-tooltip="true"
          min-width="120">
          <template slot-scope="scope">
            <div>
              <el-image style="width: 50px; height: 50px;"
                :src="scope.row.images"
                :preview-src-list="[scope.row.images]"
                fit="cover"
                preview-teleported="true" />
            </div>
          </template>
        </el-table-column>

        <el-table-column label="创建人"
          align="center"
          prop="createBy"
          min-width="140"
          show-overflow-tooltip />
        <el-table-column label="创建时间"
          align="center"
          prop="createTime"
          min-width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="更新人"
          align="center"
          prop="updateBy"
          min-width="140"
          show-overflow-tooltip />
        <el-table-column label="更新时间"
          align="center"
          prop="updateTime"
          min-width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="备注"
          align="center"
          prop="remark"
          :show-overflow-tooltip="true"
          min-width="160" /> -->

        <el-table-column label="操作"
          align="center"
          class-name="small-padding fixed-width"
          fixed="right"
          min-width="100">
          <template slot-scope="scope">
            <el-button size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body>
      <el-form ref="form"
        :model="form"
        :rules="rules"
        label-width="100px">

        <el-form-item label="名称"
          prop="famousName">
          <el-input v-model.trim="form.famousName"
            maxlength="50"
            placeholder="请输入名称">
          </el-input>
        </el-form-item>
        <el-form-item label="头衔"
          prop="famousTitle">
          <el-input v-model.trim="form.famousTitle"
            maxlength="50"
            placeholder="请输入头衔">
          </el-input>
        </el-form-item>
        <el-form-item label="年代"
          prop="refYears">
          <el-select v-model="form.refYears"
            placeholder="请选择年代"
            clearable
            style="width: 100%">
            <el-option v-for="dict in dict.type.yj_dynasty"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="头像"
          prop="images">
          <ImageUpload :limit="1"
            v-model="form.images" />
        </el-form-item>
        <el-form-item label="简介"
          prop="brief"
          style="width: 100%;">
          <el-input type="textarea"
            v-model="form.brief"
            style="width: 100%;"
            maxlength="2000"
            placeholder="请输入简介">
          </el-input>
        </el-form-item>

        <!-- <el-form-item label="备注"
          prop="remark"
          style="width: 100%;">
          <el-input type="textarea"
            v-model.trim="form.remark"
            style="width: 100%;"
            maxlength="100"
            placeholder="请输入备注">
          </el-input>
        </el-form-item> -->

      </el-form>
      <div slot="footer"
        class="dialog-footer">
        <el-button type="primary"
          @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  getStarTalkList,
  addStarTalk,
  updateStarTalk
} from "@/api/resource/starTalk.js";
import { onInput } from '@/utils/util.js'

export default {
  name: "StarTalk",
  dicts: ['yj_dynasty'],
  data () {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null
      },
      // 数据列表
      dataList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created () {
    this.getList();
  },
  methods: {
    onInput,
    /** 查询列表 */
    getList () {
      this.loading = true;
      getStarTalkList(this.queryParams).then(
        (response) => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        famousName: null,
        famousTitle: null,
        famousDynasty: null,
        brief: null,
        images: null,
        // remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = '新增名嘴资料';
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      this.form = JSON.parse(JSON.stringify(row));
      this.open = true;
      this.title = '修改名嘴资料';
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.famousId != undefined) {
            updateStarTalk(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addStarTalk(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }
  },
};
</script>
